/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import xml2js from './lib/xml2js.js';

let message = "";
let xml = '<root><text>Hello xml2js!</text><Foo:Bar/><test1>123.123</test1><test2>true</test2></root>';
let xml2 = '<foo> test </foo>'

const forNumber3 = 100;

//将名称转换为小写。Option.normalize（设置为时自动使用true）
function parseString_test_01() {
  let element;
  let startTime = Date.now();
  for (let i = 0; i < forNumber3; i++) {
    xml2js.parseString(xml, {
      tagNameProcessors: [xml2js.processors.normalize],
      attrNameProcessors: [xml2js.processors.normalize],
      valueProcessors: [xml2js.processors.normalize],
      attrValueProcessors: [xml2js.processors.normalize]
    }, (err, result) => {
      element = JSON.stringify(result)
    });
  }
  let endTime = Date.now();
  print(`dom4j_parseString: ${endTime - startTime} ms`)
  if (element != '{"root":{"text":["hello xml2js!"],"foo:bar":[""],"test1":["123.123"],"test2":["true"]}}') throw Error(`parseString != '{"root":{"text":["hello xml2js!"],"foo:bar":[""],"test1":["123.123"],"test2":["true"]}}'`);
}
function parseString_test_02() {
  let element;
  let startTime = Date.now();
  for (let i = 0; i < forNumber3; i++) {
    // 将名称转换为大写
    xml2js.parseString(xml, {
      tagNameProcessors: [nameToUpperCase],
      attrNameProcessors: [nameToUpperCase],
      valueProcessors: [nameToUpperCase],
      attrValueProcessors: [nameToUpperCase]
    }, (err, result) => {
      element = JSON.stringify(result)
    });
  }
  let endTime = Date.now();
  print(`dom4j_parseString(nameToUpperCase): ${endTime - startTime} ms`)
  if (element != '{"ROOT":{"TEXT":["HELLO XML2JS!"],"FOO:BAR":[""],"TEST1":["123.123"],"TEST2":["TRUE"]}}') throw Error(`parseString != '{"ROOT":{"TEXT":["HELLO XML2JS!"],"FOO:BAR":[""],"TEST1":["123.123"],"TEST2":["TRUE"]}}'`);

}

function parseString_test_03() {
  let element;
  let startTime = Date.now();
  for (let i = 0; i < forNumber3; i++) {
    // 将第一个字符转换为小写。例如：“MyTagName”变成“myTagName”
    xml2js.parseString(xml, {
      tagNameProcessors: [xml2js.processors.firstCharLowerCase],
      attrNameProcessors: [xml2js.processors.firstCharLowerCase],
      valueProcessors: [xml2js.processors.firstCharLowerCase],
      attrValueProcessors: [xml2js.processors.firstCharLowerCase]
    }, (err, result) => {
      element = JSON.stringify(result)
    });
  }
  let endTime = Date.now();
  print(`dom4j_parseString(firstCharLowerCase): ${endTime - startTime} ms`)
  if (element != '{"root":{"text":["hello xml2js!"],"foo:Bar":[""],"test1":["123.123"],"test2":["true"]}}') throw Error(`parseString != '{"root":{"text":["hello xml2js!"],"foo:Bar":[""],"test1":["123.123"],"test2":["true"]}}'`);;

}
function parseString_test_04() {
  let element;
  let startTime = Date.now();
  for (let i = 0; i < forNumber3; i++) {
    // 去除xml命名空间前缀。例如<foo:Bar/>将变为“Bar”。（注意：xmlns前缀没有被剥离。）
    xml2js.parseString(xml, {
      tagNameProcessors: [xml2js.processors.stripPrefix],
      attrNameProcessors: [xml2js.processors.stripPrefix],
      valueProcessors: [xml2js.processors.stripPrefix],
      attrValueProcessors: [xml2js.processors.stripPrefix]
    }, (err, result) => {
      element = JSON.stringify(result)
    });
  }
  let endTime = Date.now();
  print(`dom4j_parseString(stripPrefix): ${endTime - startTime} ms`)
  if (element != '{"root":{"text":["Hello xml2js!"],"Bar":[""],"test1":["123.123"],"test2":["true"]}}') throw Error(`parseString != '{"root":{"text":["Hello xml2js!"],"Bar":[""],"test1":["123.123"],"test2":["true"]}}'`);;

}
function parseString_test_05() {
  let element;
  let startTime = Date.now();
  for (let i = 0; i < forNumber3; i++) {
    // 将类似整数的字符串解析为整数，将类似浮点字符串解析为浮点数 例如“0”变为0，“15.56”变为15.56
    xml2js.parseString(xml, {
      tagNameProcessors: [xml2js.processors.parseNumbers],
      attrNameProcessors: [xml2js.processors.parseNumbers],
      valueProcessors: [xml2js.processors.parseNumbers],
      attrValueProcessors: [xml2js.processors.parseNumbers]
    }, (err, result) => {
      element = JSON.stringify(result)
    });
  }
  let endTime = Date.now();
  print(`dom4j_parseString(parseNumbers): ${endTime - startTime} ms`)
  if (element != '{"root":{"text":["Hello xml2js!"],"Foo:Bar":[""],"test1":[123.123],"test2":["true"]}}') throw Error(`parseString != '{"root":{"text":["Hello xml2js!"],"Foo:Bar":[""],"test1":[123.123],"test2":["true"]}}'`);;

}
function parseString_test_06() {
  let element;
  let startTime = Date.now();
  for (let i = 0; i < forNumber3; i++) {
    // 将类似布尔值的字符串解析为布尔值 例如“真”变为真，“假”变为假
    xml2js.parseString(xml, {
      tagNameProcessors: [xml2js.processors.parseBooleans],
      attrNameProcessors: [xml2js.processors.parseBooleans],
      valueProcessors: [xml2js.processors.parseBooleans],
      attrValueProcessors: [xml2js.processors.parseBooleans]
    }, (err, result) => {
      element = JSON.stringify(result)
    });
  }
  let endTime = Date.now();
  print(`dom4j_parseString(parseBooleans): ${endTime - startTime} ms`)
  if (element != '{"root":{"text":["Hello xml2js!"],"Foo:Bar":[""],"test1":["123.123"],"test2":[true]}}') throw Error(`parseString != '{"root":{"text":["Hello xml2js!"],"Foo:Bar":[""],"test1":["123.123"],"test2":[true]}}'`);;

}

function nameToUpperCase(name) {
  return name.toLocaleUpperCase();
}

function parseStringPromise_test() {
  let element;
  let parser = xml2js.Parser( /* options */
    { trim: true });
  let abc;
  let startTime = Date.now();
  for (let i = 0; i < forNumber3 * 10; i++) {
    abc = parser.parseStringPromise(xml2);
  }
  let endTime = Date.now();
  print(`dom4j_parseStringPromise: ${endTime - startTime} ms`)

  abc.then((result) => {
    element = JSON.stringify(result)
    // print(`--2-promiseParse--Done, ${element}`)
    if (element != '{"foo":"test"}') throw Error(`parseStringPromise != '{"foo":"test"}'`);

  }).catch((err) => {
    // Fail
  })

}

parseString_test_01();
parseString_test_02();
parseString_test_03();
parseString_test_04();
parseString_test_05();
parseString_test_06();
parseStringPromise_test();